package com.university.trade.util;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;

public class RequestUtil {
	private static Logger logger = Logger.getLogger(RequestUtil.class.getName());
	/**
	 * 验证请求是否是非法请求,满足以下两点
	 * 规则：
	 * 1.当前请求时间 和 服务器时间  间隔在 30秒之内  时间格式如 yyyyMMddHHmmss
	 * 2.密文和加密后的密文匹配
	 * 满足以上两点,可以视为有效请求
	 * @return
	 * @throws Exception
	 */
	public static Map<String,Object> verifyRequest(HttpServletRequest request) throws Exception{
		Map<String,Object> verifyMap = new HashMap<String,Object>();
		verifyMap.put("result", SystemConstantData.RESULT_FAIL);
		
		PropertiesUtil systemProperties = new PropertiesUtil("/systemConfig.properties");
		String system_private_key = systemProperties.getProperty("system_private_key");
		try {
			String system_open_test = systemProperties.getProperty("system_open_test");//是否打开测试环境
			if(!"T".equals(system_open_test)){
				if(null == system_private_key || "".equals(system_private_key)){
					logger.info("---系统设置被篡改,抱歉,暂时不能为你服务!!------");
					verifyMap.put(SystemConstantData.PROMPT_MESSAGE, "系统设置被篡改,抱歉,暂时不能为你服务!!");
					return verifyMap;
				}
				Date requestDate = paramDateParse(request.getParameter("requestDate"));
				if(requestDate == null){
					logger.info("---请求参数缺失时间戳,请核对!!------");
					verifyMap.put(SystemConstantData.PROMPT_MESSAGE, "本次请求参数缺失时间戳参数,请核对!!");
					return verifyMap;
				}
				Date currentDate = new Date();
				// 1.判断本次请求 是否超过 30秒
				if(null == systemProperties.getProperty("system_timeout_value") || Math.abs(DateUtil.modifyTime(requestDate, currentDate)) > Long.parseLong(systemProperties.getProperty("system_timeout_value"))){
					logger.info("---请求超时,请重试!!------");
					verifyMap.put(SystemConstantData.PROMPT_MESSAGE, "请求超时,请重试!!");
					return verifyMap;
				}
				String sign = paramStringParse(request.getParameter("sign"));
				EncryptionDecryption des = new EncryptionDecryption(system_private_key);// 自定义密钥
				String encryptRequestDate =  des.encrypt(des.encrypt(request.getParameter("requestDate")));
				//2.判断 加密后的时间戳  是否匹配
				if(StringUtil.isEmpty(sign)){
					verifyMap.put(SystemConstantData.PROMPT_MESSAGE, "非法请求,请重试!!");
					return verifyMap;
				}else if(sign.equals(encryptRequestDate)){//如果密文匹配，则是有效的请求
					verifyMap.put("result", SystemConstantData.RESULT_OK);
					verifyMap.put(SystemConstantData.PROMPT_MESSAGE, "有效的请求!!");
					return verifyMap;
				}else{
					verifyMap.put(SystemConstantData.PROMPT_MESSAGE, "未知错误,请重新登录!!");
					return verifyMap;
				}
			}else{
				verifyMap.put("result", SystemConstantData.RESULT_OK);
				verifyMap.put(SystemConstantData.PROMPT_MESSAGE, "有效的请求!!");
				return verifyMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
			return verifyMap;
		}
	}
	/**
	 * double精度装换
	 * @param param
	 * @return
	 */
	public static double paramDoubleParse(String param){
		if(param == null || "".equals(param)){
			return 0.0d;
		}else{
			return Double.valueOf(param);
		}
	}
	/**
	 * int数据装换
	 * @param param
	 * @return
	 */
	public static int paramIntParse(String param){
		if(param == null || "".equals(param)){
			return 0;
		}else{
			return Integer.valueOf(param);
		}
	}
	/**
	 * long数据装换
	 * @param param
	 * @return
	 */
	public static long paramLongParse(String param){
		if(param == null || "".equals(param)){
			return 0L;
		}else{
			return Long.valueOf(param);
		}
	}
	/**
	 * String数据装换
	 * @param param
	 * @return
	 */
	public static String paramStringParse(String param){
		if(param == null || "".equals(param) || "".equals(param.trim())){
			return "";
		}else{
			return param.trim();
		}
	}
	/**
	 * Date数据装换
	 * @param param
	 * @return
	 */
	public static Date paramDateParse(String param){
		if(param == null || "".equals(param)){
			return null;
		}else{
			try {
				SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
				return sdf.parse(param);
			} catch (Exception e) {
				e.printStackTrace();
				return null;
			}
		}
	}
}
